package com.ruoyi.quartz.task;

import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.service.IAppUserService;
import com.ruoyi.bizsys.service.ICreditAccountService;
import com.ruoyi.bizsys.service.ICustomerInfoService;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.business.service.ILoanService;
import com.ruoyi.business.service.IPayTransService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.extdata.common.config.PayStackConfig;
import com.ruoyi.extdata.service.IExternalApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 放款交易结果查询任务
 *
 * @author Jamnsin
 */
@Slf4j
@Component("payTransTask")
public class PayTransTask {

    @Autowired
    ILoanOrderService loanOrderService;

    @Autowired
    IPayTransService payTransService;

    @Autowired
    IExternalApiService externalApiService;

    @Autowired
    PayStackConfig payStackConfig;

    @Autowired
    ICreditAccountService creditAccountService;

    @Autowired
    IAppUserService appUserService;

    @Autowired
    ILoanService loanService;

    @Autowired
    ICustomerInfoService customerInfoService;

    public void payTransQuery() {
        log.info("放款中状态查询交易结果任务 Start >>>>>>>>>>");

        LoanOrder loanOrder = new LoanOrder();
        loanOrder.setLoanStatus(LoanOrder.LOAN_STATUS_3_LOAN_PENDING);
        loanOrder.setState(BizConstants.BIZ_STATUS_1);
        List<LoanOrder> loanOrderList = loanOrderService.selectLoanOrderList(loanOrder);
        log.info("待处理的状态为放款中的数据为：{} 条", loanOrderList.size());

        if (CollectionUtils.isEmpty(loanOrderList)) {
            log.info("放款中状态查询交易结果任务 End >>>>>>>>>>");
            return;
        }

        loanOrderList.stream().forEach(p -> {
//            String custNo = p.getCustNo();
//            CreditAccount creditAccount = new CreditAccount();
//            creditAccount.setCustNo(custNo);
//            creditAccount = creditAccountService.selectCreditAccountList(creditAccount).get(0);
//
//            CustomerInfo customerInfo = new CustomerInfo();
//            customerInfo.setCustNo(custNo);

//            AppUser appUser = new AppUser();
//            appUser.setUserUuid(customerInfoService.selectCustomerInfoList(customerInfo).get(0).getUserId());
//            appUser = appUserService.selectAppUserList(appUser).get(0);

            try {
                loanService.verifyTransfer(p, false);
            } catch (Exception e) {
                if (e instanceof BusinessException) {
                    BusinessException be = (BusinessException) e;
                    if (RspEnmu.LOAN_PENDING.getCode().equals(be.getCode())) {
                        return;
                    }
                }
                log.error("查询代付交易异常，", e);
            }
        });

        log.info("放款中状态查询交易结果任务 End >>>>>>>>>>");
    }
}
